package controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import com.google.gson.Gson;

import snake.GameEngine;
import snake.GamePackage;
import snake.Player;
import snake.Player.Direction;


@ServerEndpoint("/testCont")
public class WebSocketController
{
	public static Map<Integer,GameEngine> gameEngineList =  new HashMap<Integer,GameEngine>();
	
	@OnOpen
    public void handleOpen(Session session)
	{
		System.out.println("Connected to Individual Chat Server");
    }
	
	@OnMessage
    public static void handleMessage(String message, Session session)
	{     
		System.out.println(message);
		int gameId=-1;
		String direction=null;
		int userId=-1;

		String m[] = message.split(":");
			
		gameId = Integer.parseInt(m[0]);
		direction= m[1];
		userId = Integer.parseInt(m[2]);

		
		GameEngine ge = null;
		
		if(gameEngineList.get(gameId) != null)
		{
			ge = gameEngineList.get(gameId);
			
			Player player = null;

			if(!ge.getSessionList().contains(session))
			{
				session.getUserProperties().put("gameId", gameId);  //adauga pe sesiune id-ul jocului
				ge.addSession(session);
			}
			else
			{		
				for(Player p : ge.getGamePackage().getPlayersList())
				{
					if(p.getId() == userId)
					{
						player = p;
					}
				}
				
				if (Direction.valueOf(direction) == Direction.NORTH && player.getDirection() == Direction.SOUTH
						|| Direction.valueOf(direction) == Direction.EAST && player.getDirection() == Direction.WEST
						|| Direction.valueOf(direction) == Direction.SOUTH && player.getDirection() == Direction.NORTH
						|| Direction.valueOf(direction) == Direction.WEST && player.getDirection() == Direction.EAST)
				{
				} 
				else
				{
					Direction dr = Direction.valueOf(direction);
					player.setDirection(dr);
				}
			}
		}
	}
	
	public static void sendMessage(GamePackage gamePackage, Set<Session> sessionList)
	{
		Gson gson = new Gson();
		String str = gson.toJson(gamePackage);
		
		for(Session session : sessionList)
		{
			try
			{
				session.getBasicRemote().sendText(str);
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}
	
	@OnClose
	public void handleClose(Session session)
	{
	
		if(session.getUserProperties().get("gameId") != null)
		{
			int gameId = (int) session.getUserProperties().get("gameId");
			
			if(gameEngineList.containsKey(gameId))
			{
				GameEngine ge = gameEngineList.get(gameId);
				
				if(ge.getSessionList().contains(session))
				{
					//System.out.println("");
					session.getUserProperties().remove("gameId");
					ge.getSessionList().remove(session);
					
					if(ge.getGamePackage().getPlayersList().size() <= 0)
					{
						gameEngineList.remove(gameId);
					}
				}
			}
		}
		//chatUsers.remove(session);
	}

}
